package io.renren.modules.project.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.List;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.SmsUtil;
import io.renren.modules.project.entity.ProjectExpachEntity;
import io.renren.modules.project.entity.ProjectSituationEntity;
import io.renren.modules.project.service.ProjectExpachService;
import io.renren.modules.project.service.ProjectSituationService;
import io.renren.modules.project.vo.ExpachVoEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 成果交接表
 *
 * @author ygg
 * @date 2021-06-08 15:05:33
 */
@RestController
@RequestMapping("project/expach")
public class ProjectExpachController {

    @Autowired
    private ProjectExpachService projectExpachService;

    @Autowired
    private ProjectSituationService projectSituationService;

    @Autowired
    private SysUserService sysUserService;
    /**
     * 列表
     */
    @RequestMapping("/volist")
    public R page(@RequestParam Map<String, Object> params){
        List<ExpachVoEntity> volist = projectExpachService.queryVoList(params);

        return R.ok().put("volist", volist);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("project:expach:list")
    public R list(@RequestParam Map<String, Object> params){
        List<ProjectExpachEntity> list = projectExpachService.queryList(params);

        return R.ok().put("list", list);
    }

    /**
     * 获取最新的列表
     */
    @RequestMapping("/lastList")
//    @RequiresPermissions("project:expach:list")
    public R lastList(@RequestParam Map<String, Object> params){
        List<ProjectExpachEntity> list = projectExpachService.queryLastList(params);
        Long maxCode = projectExpachService.getMaxBillcode(params);

        return R.ok().put("list", list).put("maxCode",maxCode);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		ProjectExpachEntity projectExpach = projectExpachService.selectById(id);

        return R.ok().put("projectExpach", projectExpach);
    }

    /**
     * 保存
     */
    @SysLog("保存编辑成果")
    @RequestMapping("/save")
    @RequiresPermissions("project:expach:save")
    public R save(@RequestBody ExpachVoEntity voExpach){
		projectExpachService.save(voExpach);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("project:expach:update")
    public R update(@RequestBody ProjectExpachEntity projectExpach){
		projectExpachService.updateById(projectExpach);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("project:expach:delete")
    public R delete(@RequestBody ExpachVoEntity voExpach){
		projectExpachService.deleteBatch(voExpach);

        return R.ok();
    }

    /**
     * 修改状态
     */
    @SysLog("提交成果")
    @RequestMapping("/postAch")
//    @RequiresPermissions("project:expach:update")
    public R setStage(@RequestBody Map<String, Object> params){
        String projectNo = params.get("projectNo").toString();
        ProjectSituationEntity entity = projectSituationService.queryByProjectNo(projectNo);
        entity.setIsSach(1L);
        projectSituationService.updateById(entity);

        if ((Boolean) params.get("isSmsSend")) {
            SysUserEntity userEntity = sysUserService.selectById((Integer)params.get("achUserId"));
            String postUser = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
            String[] replaceValues = new String[]{params.get("projectName").toString(),postUser};
            SmsUtil.sendPostAchMsg(userEntity.getPhonenumber(),replaceValues);
        }

        return R.ok();
    }
}
